package zcommon

import (
	"bytes"
	"io/ioutil"

	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
)

// 转 GBK 编码。
func Chg2GBK(text string) (string, error) {
	data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GBK.NewEncoder()))
	if err != nil {
		return "", err
	}
	return string(data), nil
}

// 转 UTF 编码。
func Chg2UTF(text string) (string, error) {
	data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(text)), simplifiedchinese.GBK.NewDecoder()))
	if err != nil {
		return "", err
	}
	return string(data), nil
}
